Skip to content

feat: implement TypeFlavor and enhance inline assembly operand handling#282

Merged
LunaStev merged 1 commit intowavefnd:masterfrom
LunaStev:feat/introduce-TypeFlavor-and-enhance-inline-assembly-operand-handling
Feb 5, 2026
Merged

feat: implement TypeFlavor and enhance inline assembly operand handling#282
LunaStev merged 1 commit intowavefnd:masterfrom
LunaStev:feat/introduce-TypeFlavor-and-enhance-inline-assembly-operand-handling

Conversation

@LunaStev
Copy link
Member

@LunaStev LunaStev commented Feb 5, 2026

This PR introduces a significant enhancement to Wave's type system and LLVM backend by implementing "Type Flavors." This architectural change allows the compiler to distinguish between high-level value representations and ABI-compliant types required for C FFI. Additionally, the inline assembly (asm) engine has been overhauled to handle register bit-width normalization and sign-aware operand promotion, making system-level programming more predictable and robust.

Key Changes

1. Type Flavoring & ABI Compatibility

  • TypeFlavor Enum: Introduced TypeFlavor in types.rs with two variants: Value (internal representation) and AbiC (C ABI compliant).
  • Context-Aware Mapping: Updated wave_type_to_llvm_type to adjust types based on the flavor. For example, when using AbiC, Booleans are now emitted as i8 rather than i1 to ensure correct parameter passing when calling external C functions.
  • System-Wide Integration: Refactored all codegen entry points—including function signatures, struct definitions, and variable declarations—to utilize the appropriate flavor based on the context.

2. Robust Inline Assembly (asm) Improvements

  • Sign-Aware Input Promotion: Implemented automatic integer promotion for assembly inputs. The compiler now uses the infer_signedness utility to decide between sign-extension (sext) and zero-extension (zext) when promoting values to match target register widths.
  • Output Normalization: Added logic to handle bit-width mismatches for assembly outputs. If a target register (e.g., a 64-bit 'r' constraint) returns a value larger than the destination variable, the backend now correctly truncates or extends the result.
  • Enhanced Coercion: Refactored coerce_basic_value_for_store to support a wider range of conversions for assembly results, including complex integer-to-pointer and float-to-int mappings.

3. LLVM Backend Utilities

  • Recursive Literals: Added support for recursive literal generation when the expected target is an ArrayType. This allows complex nested initializers to be generated more reliably.
  • Signedness Inference: Introduced the infer_signedness helper to assist the codegen engine in making mathematically correct decisions during type promotion and comparison.

4. Maintenance & Hygiene

  • Cleaned up unused imports across the parser and codegen submodules.
  • Improved internal documentation and error messages within the backend to assist with debugging complex IR generation scenarios.

Impact

Before: Passing a bool to a C function might result in i1 being passed, which some C ABIs handle inconsistently.
After: With TypeFlavor::AbiC, the bool is correctly passed as an i8 (0 or 1), matching standard C expectations.

Benefits

  • FFI Reliability: Better compatibility with external C libraries by respecting standard ABI type widths.
  • Low-Level Safety: Predictable register handling in asm blocks prevents subtle bugs related to sign-extension and bit-truncation.
  • Code Quality: A more modular and context-aware type system simplifies future backend development.

This commit introduces the concept of "TypeFlavor" to distinguish between
high-level value types and ABI-compliant types during LLVM IR generation.
It also significantly improves the robustness of inline assembly by
implementing operand bit-width normalization and sign-aware extension.

Changes:
- **Type Flavoring & ABI Compatibility**:
  - Introduced `TypeFlavor` (Value vs. AbiC) in `types.rs`.
  - Updated `wave_type_to_llvm_type` to handle context-specific types;
    for example, Booleans are now emitted as `i8` when using `TypeFlavor::AbiC`
    to ensure C ABI compatibility.
  - Refactored all codegen entry points (functions, structs, variables)
    to utilize these flavors.
- **Enhanced Inline Assembly**:
  - Implemented automatic sign/zero extension for inline assembly input
    operands. The compiler now infers signedness from the source expression
    to decide between `sext` and `zext` when promoting integers to
    register widths.
  - Added operand normalization for assembly outputs. If a target register
    has a specific bit width (e.g., 64-bit for 'r' constraints), the
    compiler now correctly truncates or extends the result to match the
    destination variable.
  - Refactored `coerce_basic_value_for_store` to handle a wider range
    of integer-to-pointer and float-to-int conversions for assembly outputs.
- **LLVM Backend Improvements**:
  - **Literals**: Added support for recursive literal generation when
    an `ArrayType` is the expected target type.
  - **Type Inference**: Added `infer_signedness` utility to assist
    in correct integer promotion during codegen.
- **Cleanup**:
  - Removed unused imports and clarified internal documentation in
    the parser and codegen modules.

These changes provide more predictable behavior when interfacing with
system-level assembly and ensure better compatibility with external C
libraries.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev merged commit b012dae into wavefnd:master Feb 5, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant